//
//  CustomTabbarController.m
//  Mtime
//
//  Created by wang xinkai on 15/7/28.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "CustomTabbarController.h"

@interface CustomTabbarController ()

@end

@implementation CustomTabbarController


- (void)userInteractionEnabled {

    _contentView.userInteractionEnabled = YES;

}


- (void)viewDidLoad {
    [super viewDidLoad];


//    [self.tabBar addSubview:contentView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInteractionEnabled) name:@"userInteractionEnabled" object:nil];
    
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 49)];

    _contentView.userInteractionEnabled = NO;
    
    [_contentView setBackgroundColor:[UIColor orangeColor]];
    [_contentView setBackgroundColor:[UIColor colorWithPatternImage:[[UIImage imageNamed:@"pic_title_bg.9"] stretchableImageWithLeftCapWidth:1 topCapHeight:20]]];
    [self.tabBar addSubview:_contentView];
    
}


-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    [self removeTabbarItems];

}

//移除原有的button
-(void)removeTabbarItems{
    
    
    for (UIView *v in self.tabBar.subviews) {
        
//        从字符串中 提取类
        if ([v isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
         
            [v removeFromSuperview];
            
        }
    }
    
}



//重写 set方法
-(void)setViewControllers:(NSArray *)viewControllers{

    [super setViewControllers:viewControllers];
    
    [self createBtns];
}


- (void)pushaction:(NSInteger)index {
    
    UIButton *btn = (UIButton *)[_contentView viewWithTag:100 +index];
    
    [self act:btn];
}


//btn调用的方法
-(void)act:(UIButton *)btn
{
    [self setSelectedIndex:btn.tag - 100];
}
//添加button
-(void)createBtns{
    
    
    for (int i = 0; i<self.viewControllers.count; i++) {
        
        
//        计算frame
        CGRect frame = CGRectMake(kScreenW/self.viewControllers.count*i, 0, kScreenW/self.viewControllers.count, 49);
        
//        初始化btn
        CNTabbarItem *btn = [[CNTabbarItem alloc] initWithFrame:frame tabbarItem:[[self.viewControllers objectAtIndex:i] tabBarItem]];
        
//        添加
        [_contentView addSubview:btn];
        
//        设置tag
        btn.tag = 100+i;
        
//        添加方法
        [btn addTarget:self action:@selector(act:) forControlEvents:UIControlEventTouchDown];
        
        
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@implementation CNTabbarItem

-(id)initWithFrame:(CGRect)frame tabbarItem:(UITabBarItem*)item{

    if (self = [super initWithFrame:frame]) {
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-15)];
        
        imageView.image = item.image;
//     布局方式
        imageView.contentMode = UIViewContentModeCenter;
        
        [self addSubview:imageView];
        
        
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-15, self.frame.size.width, 15)];
        
        titleLabel.text = item.title;
//        对其方式
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize:12];
        
        
        [self addSubview:titleLabel];
        
        
        
        
        
    }
    
    return self;
}




@end


